<?php
namespace app\home\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;

/**
 *
 * 实名认证
 */
class Certification extends Base{
	public function index(){
		// echo Session::get('uid');
		$user = $this->userInformation();
		if($user['RNA']==2){
			$cer = Db::name('user_certification')->where(['uid'=>Session::get('uid'),'status'=>0])->order('id','desc')->field('name,IDnumber')->find();
			return view('Certification/index',['user'=>$user,'cer'=>$cer]);
		}elseif($user['RNA']==3){
            $cer = Db::name('user_certification')->where(['uid'=>Session::get('uid'),'status'=>2])->order('id', 'desc')->field('name,IDnumber,IDpositive,IDback,remark')->find();
            return view('Certification/index',['user'=>$user,'cer'=>$cer]);
        }elseif($user['RNA']==1){
            $cer = Db::name('user_certification')->where(['uid'=>Session::get('uid'),'status'=>1])->order('id', 'desc')->field('name,IDnumber')->find();
            return view('Certification/index',['user'=>$user,'cer'=>$cer]);
        }
		return view('Certification/index',['user'=>$user]);
	}

	//提交
	public function addSubmit(){
		$param = myarr($this->request->post());
	
		if($param['infoUserName']==null) return ['msg'=>'名字不能为空'];
        if($param['infoUserCard']==null) return ['msg'=>'身份证不能为空'];
//		$file = request()->file('img');
//		$files = request()->file('img2');
        $str=$_POST['cardImagestr1'];
        $str2=$_POST['cardImagestr2'];
        if(!$str){
            $this->error('照片不能为空','Certification/index');
        }
        if(!$str2){
            $this->error('照片不能为空','Certification/index');
        }
        $idcard=Db::name('users')->find(Session::get('uid'))['idcard'];
        if($idcard){
            if($idcard!=$param['infoUserCard']){
                return "<script>alert('与注册身份证不一致');window.location.href=self.location=document.referrer;</script>";
            }
        }
        if($param['codes']!=Session::get('codes')){
            $this->error('资料已经提交请勿重复提交','Certification/index');
        }
        Session::set('codes',mt_rand(1000,9999));
        $base64_string= explode(',', $str); //截取data:image/png;base64, 这个逗号后的字符
        $data=base64_decode($base64_string[1]);
        $date = date('Ymd');
        $name1 = md5(mt_rand(1000,9999).time()).'.'.'jpg';
        $name2 = md5(mt_rand(1000,9999).time()).'.'.'jpg';
        if(!file_exists('upload/certification/'.$date)){
            mkdir('upload/certification/'.$date);
        }
        file_put_contents('upload/certification/'.$date.'/'.$name1,$data);

        $base64_string= explode(',', $str2); //截取data:image/png;base64, 这个逗号后的字符
        $data=base64_decode($base64_string[1]);
        file_put_contents('upload/certification/'.$date.'/'.$name2,$data);
		$data = [
			'uid'=>Session::get('uid'),
			'name'=>$param['infoUserName'],
			'IDnumber'=> $param['infoUserCard'],
            'IDpositive'=>$date.'/'.$name1,
            'IDback'=>$date.'/'.$name2,
		];

//		if($file==''|| $files==''){
//			$this->error('认证失败，必须上传图片','Certification/index');
//		}
//		$info = $file->validate(['size'=>3300*1000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload/certification');
//        $infos = $files->validate(['size'=>3300*1000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'upload/certification');
//        if($info&&$infos){
//            $info->getExtension();
//            $infos->getExtension();
//            $img=$info->getFilename();
//            $imgs=$infos->getFilename();
//            $data['IDpositive'] = date('Ymd').'/'.$img;
//            $data['IDback'] = date('Ymd').'/'.$imgs;
//
//        }else{
//        	$this->error('上传失败，请检查图片大小或网络连接','Certification/index');
//        }

        //启动事务    
       	Db::startTrans();
        try{ 
	        $config = Db::name('config')->where('key','rna')->find();
	        $mobile = $config['value'];
	        $tpCode = "SMS_137070025";
	        $tplParam = 'SMRZ';
	        $da = Db::name('user_certification')->insert($data);
	        $up = Db::name('users')->where(['id'=>Session::get('uid')])->update(['RNA'=>2]);
        Db::commit();
        	//$message = sendMsgss($mobile,$tpCode,$tplParam);
      	} catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('信息提交失败，请检查提信息和网络连接','Certification/index');
        }   

        $this->success('信息提交成功','Certification/index'); 


	}

	//sfz
	public function idVerification(){
        $param = myarr($this->request->post());
		$code = Db::name('user_certification')->where(['IDnumber'=>preg_replace('# #', '', $param['infoUserCard']),'status'=>1])->find();
		if($code){
			return ['msg'=>'error'];
		}else{
			return ['msg'=>'success'];
		}
	}

	public function refreshCer(){
	    $refreshCer = Db::name('users')->where('id',Session::get('uid'))->update(['RNA'=>0]);
        if($refreshCer){
            return ['msg'=>'success'];
        }else{
            return ['msg'=>'更新失败'];
        }
    }

}